package com.test.clyq.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.test.Constants;
import com.test.clyq.entity.CategoryImg;
import com.test.clyq.entity.Users;
import com.test.clyq.form.CategoryImgForm;
import com.test.clyq.service.CategoryImgService;
import com.test.json.JsonUtil;
import com.test.util.FileUtils;
import com.test.util.FormatExceptionStack;
import com.test.util.ObjectUtils;
import com.test.util.Pagination;
import com.test.util.SessionUtil;
import com.test.util.StringUtils;
import com.test.webapp.action.BaseAction;

/**
 * CategoryImgAction. <br>
 * 栏目域图片管理.
 * <p>
 * Copyright: Copyright (c) 2010-4-17 下午04:52:10
 * <p>
 * Company: 北京宽连十方数字技术有限公司
 * <p>
 * Author: liuxl@c-platform.com
 * <p>
 * Version: 1.0
 * <p>
 */
public class CategoryImgAction extends BaseAction {

	/**
	 * 添加一个栏目域图片
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward addCategoryImg(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("添加一个栏目域图片表start");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		CategoryImgForm categoryImgForm = (CategoryImgForm) form;
		try {
			// String categoryImgId = getParameter(request, "categoryImgId");
			String categoryId = "0";
			Users user = SessionUtil.getUserFromSession(request);
			if (!ObjectUtils.isNull(user) && StringUtils.isNumeric(categoryId)) {
				CategoryImg img = new CategoryImg();

				FormFile f = categoryImgForm.getCategoryImgFile();
				if (f.getFileName() != null && !f.getFileName().equals("")) {
					String path = FileUtils.upload((String) Constants.settings.get("UPLOAD_DIR"), f);
					img.setCategoryImgPath(path);
				}
				/** 是否是专题链接 默认0 不是 */
				img.setIsZhuanTi(!StringUtils.isEmpty(getParameter(request, "isZhuanTi")) ? Integer.valueOf(getParameter(request, "isZhuanTi")) : 0);
				img.setCategoryImgDesc(categoryImgForm.getCategoryImgDesc());
				img.setCategoryImgName(categoryImgForm.getCategoryImgName());
				img.setCategoryImgUrl(categoryImgForm.getCategoryImgUrl());
				img.setCategoryImgCreatetime(System.currentTimeMillis());
				img.setCategoryId(Long.valueOf(categoryId));
				categoryImgService.saveOrUpdate(img, null);
				this.log.debug("添加一个栏目域图片成功");
				this.log.info("添加一个栏目域图片end");
				print(response, true);

			} else {
				print(response, false);
			}

		}
		catch (Exception e) {
			this.log.error("添加一个栏目域图片:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 删除栏目域图片
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward deleteCategoryImg(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("删除栏目域图片start");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		try {
			String categoryImgId = getParameter(request, "categoryImgId");
			Users user = SessionUtil.getUserFromSession(request);
			if (StringUtils.isNumeric(categoryImgId) && !ObjectUtils.isNull(user)) {
				categoryImgService.delCategoryImgById(Long.valueOf(categoryImgId), user);
				this.log.debug("删除栏目域图片成功");
				this.log.info("删除栏目域图片end");
				print(response, true);
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("删除栏目域图片:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;

	}

	/**
	 * 获得一个栏目域图片
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getCategoryImg(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得一个栏目域图片表start");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		try {
			String categoryImgId = request.getParameter("categoryImgId");
			CategoryImg img = null;
			if (StringUtils.isNumeric(categoryImgId)) {
				img = categoryImgService.getCategoryImgById(Long.valueOf(categoryImgId));
				if (ObjectUtils.isNull(img)) {
					print(response, false);
					return null;
				}
				this.log.debug("获得一个栏目域图片成功");
				this.log.info("获得一个栏目域图片end");
				print(response, "{results:[{%s}]}", null, null, img.toJSON());
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("获得一个栏目域图片:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 获得栏目域图片列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward getCategoryImgs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("获得栏目域图片列表start");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		try {
			String start = getParameter(request, "start");
			String limit = getParameter(request, "limit");
			String categoryId = "0";
			if (StringUtils.isNumeric(start) && StringUtils.isNumeric(limit) && StringUtils.isNumeric(categoryId)) {
				int currpageNo = 1;
				int pageCount = Integer.parseInt(limit);
				if (Integer.parseInt(start) / pageCount != 0) {
					currpageNo = Integer.parseInt(start) / pageCount + 1;// 当前页码
				}
				Pagination pagination = new Pagination(currpageNo, pageCount);// 设置分页参数（页码和每页显示记录条数）
				int totalCount = 0;
				List<CategoryImg> list = categoryImgService.getCategoryImgsByCategoryId(Long.valueOf(categoryId), pagination);
				totalCount = pagination.getTotalRows();
				String jsonStr = JsonUtil.getJsonStr(list, new String[] { "categoryImgId", "categoryImgName", "categoryImgUrl", "categoryImgPath",
				        "categoryImgCreatetime", "isPublic", "orderId", "isZhuanTi" }, new String[] { "categoryImgCreatetime" }, totalCount);
				this.log.debug("获得栏目域图片列表成功");
				this.log.info("获得栏目域图片列表end");
				print(response, jsonStr, null, null);
			}

		}
		catch (Exception e) {
			this.log.error("获得栏目域图片:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 是否推荐
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward isPublic(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("推荐到wap首页start");
		String categoryImgId = request.getParameter("categoryImgId");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (StringUtils.isNumeric(categoryImgId)) {
				CategoryImg categoryImg = categoryImgService.getCategoryImgById(Long.valueOf(categoryImgId));
				if (categoryImg != null) {
					if (categoryImg.getIsPublic() != null && categoryImg.getIsPublic() == 1) {
						categoryImg.setIsPublic(0);
					} else {
						categoryImg.setIsPublic(1);
					}
					categoryImgService.saveOrUpdate(categoryImg, user);
				}

			}
			this.log.debug("推荐到wap首页成功");
			this.log.info("推荐到wap首页end");
			print(response, true);
		}
		catch (Exception e) {
			this.log.error("推荐到wap首页:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 修改排序值
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updateCategoryImgIdOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("排序start");
		String categoryImgId = request.getParameter("categoryImgId");
		String orderId = request.getParameter("orderId");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		Users user = SessionUtil.getUserFromSession(request);
		try {
			if (StringUtils.isNumeric(categoryImgId)) {
				CategoryImg categoryImg = categoryImgService.getCategoryImgById(Long.valueOf(categoryImgId));
				if (categoryImg != null) {
					if (StringUtils.isNumeric(orderId)) {
						categoryImg.setOrderId(Integer.parseInt(orderId));
					} else {
						categoryImg.setOrderId(0);
					}
					categoryImgService.saveOrUpdate(categoryImg, user);
				}

			}
			this.log.debug("排序成功");
			this.log.info("排序end");
			print(response, true);
		}
		catch (Exception e) {
			this.log.error("排序:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}

	/**
	 * 修改一个栏目域图片
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	public ActionForward updatedownloadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	        throws IOException {
		this.log.info("修改一个栏目域图片表start");
		CategoryImgService categoryImgService = (CategoryImgService) getBean("categoryImgService");
		try {
			String categoryImgId = getParameter(request, "categoryImgId");
			Users user = SessionUtil.getUserFromSession(request);
			if (StringUtils.isNumeric(categoryImgId) && !ObjectUtils.isNull(user)) {
				CategoryImg img = categoryImgService.getCategoryImgById(Long.valueOf(categoryImgId));
				CategoryImgForm categoryImgForm = (CategoryImgForm) form;
				if (ObjectUtils.isNull(img)) {
					print(response, false);
				} else {
					// 不为空进行上传
					FormFile f = categoryImgForm.getCategoryImgFile();
					if (f.getFileName() != null && !f.getFileName().equals("")) {
						FileUtils.deleteFileByDir((String) Constants.settings.get("UPLOAD_DIR") + img.getCategoryImgPath());
						String path = FileUtils.upload((String) Constants.settings.get("UPLOAD_DIR"), f);
						img.setCategoryImgPath(path);
					}

					/** 是否是专题链接 默认0 不是 */
					img.setIsZhuanTi(!StringUtils.isEmpty(getParameter(request, "isZhuanTi")) ? Integer.valueOf(getParameter(request, "isZhuanTi"))
					        : 0);
					img.setCategoryImgDesc(categoryImgForm.getCategoryImgDesc());
					img.setCategoryImgName(categoryImgForm.getCategoryImgName());
					img.setCategoryImgUrl(categoryImgForm.getCategoryImgUrl());

					categoryImgService.saveOrUpdate(img, user);
					this.log.debug("修改一个栏目域图片成功");
					this.log.info("修改一个栏目域图片end");
					print(response, true);
				}
			} else {
				print(response, false);
			}
		}
		catch (Exception e) {
			this.log.error("修改一个栏目域图片:失败 " + FormatExceptionStack.formatException(e));
			print(response, false);
		}
		return null;
	}
}
